package com.groupname.dangerousdelves;

import android.graphics.RectF;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import android.view.MotionEvent;
import sofia.app.ShapeScreen;

// -------------------------------------------------------------------------
/**
 *  Write a one-sentence summary of your class here.
 *  Follow it with additional details about its purpose, what abstraction
 *  it represents, and how to use it.
 *
 *  @author Chris Fieldman (chrisf90)
 *  @version 2012.11.03
 */
public class DungeonScreen
    extends ShapeScreen
{
    //~-------------------------------------------------------------------------
    // Member Variables
    private int width;
    private int height;

    private int cellWidth;
    private int cellHeight;

    private ArrayList<ArrayList<Tile>> cells;

    private AndroidFileIO fileIO;

    //~-------------------------------------------------------------------------
    // Methods
    /**
     * Initialize the game and the GUI.
     */
    public void initialize()
    {
        this.cellWidth = (int)(getWidth() / 10.f);
        this.cellHeight = (int)(getHeight() / 18.f);

        this.fileIO = new AndroidFileIO(getApplicationContext());

        this.cells = new ArrayList<ArrayList<Tile>>();

        try
        {
            load("dungeon1.txt");
        }
        catch (IOException e)
        {
            this.cellHeight++;
            this.cellHeight--;
        }
    }

    // ----------------------------------------------------------
    /**
     * Loads a dungeon from a file.
     * @param file The file to load
     * @throws IOException Pass any exceptions to the caller.
     */
    public void load(String file) throws IOException
    {
        BufferedReader reader = this.fileIO.readAsset(file);
        String line = reader.readLine();
        TileSelector selector = new TileSelector();

        RectF rect = new RectF();
        rect.top = 0;
        rect.bottom = this.cellHeight;
        rect.left = 0;
        rect.right = this.cellWidth;

        while (!line.toString().equals("end"))
        {
            ArrayList<Tile> row = new ArrayList<Tile>();
            for (int i = 0; i < line.length(); i++)
            {
                Tile tile = selector.createTile(line.charAt(i), rect);
                row.add(tile);
                add(tile.getShape());

                rect.left += this.cellWidth;
                rect.right += this.cellWidth;
            }

            rect.left = 0;
            rect.right = this.cellWidth;
            rect.top += this.cellHeight;
            rect.bottom += this.cellHeight;

            this.cells.add(row);
            line = reader.readLine();
        }

        // Dungeons are assumed to be rectangular.
        this.width = this.cells.size();
        this.height = this.cells.get(0).size();
    }

    // ----------------------------------------------------------
    /**
     * Place a description of your method here.
     * @param event
     */
    public void onTouchDown(MotionEvent event)
    {
        // temp
    }
}